// pages/yuetixian/yuetixian.js
const utilbase = require('../../../../utils/setpwdbase64');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    personname: '',
    jine: '',
    scrIndex: 0,
    tihuoWay: '请选择你要提现到的银行', //默认选项
    weixininfo: '',
    yinhangkainfo: '',
    showModal: false, //add弹窗
    mima: '', //密码
    isFocus: false, //控制input 聚焦
    type: 1, //2提现到银行卡1提现到微信
    allmoney:'',
  },
  //忘记密码
  towangji(){
    wx.navigateTo({
      url: '/pages/setting/page/shezhi/jiaoyishezhi/jiaoyishezhi',
    })
  },
  // 获取焦点
  getjiaodian() {
    this.setData({
      isFocus: true
    })
  },
  // 输入密码发送请求
  setpassword(e) {
    let that = this
    that.setData({
      mima: e.detail.value
    })
    let account, bankName, bindId
    if (that.data.type==1){
      account = that.data.yinhangkainfo.account
      bankName = that.data.yinhangkainfo.bname
      bindId = that.data.yinhangkainfo.id
    }else{
      account = that.data.weixininfo.account
      bankName = that.data.weixininfo.bname
      bindId = that.data.weixininfo.id
    }
    if (e.detail.value.length > 5) {
      wx.request({
        url: app.globalData.myurl,
        data: {
          cmd: "applyShopWhd",
          shopId: wx.getStorageSync('shopId'),
          userId: wx.getStorageSync('userId'),
          amount: that.data.jine,
          type: that.data.type,
          // appType:3,
          account: account,
          bankName: bankName,
          accountName: that.data.personname,
          tpassword: utilbase.base64encode(utilbase.utf16to8(that.data.mima)),
          bindId: bindId
        },
        header: {
          "content-type": "application/x-www-form-urlencoded",
          "token": wx.getStorageSync('token')
        },
        success(res) {
          if (res.data.zjstate == 1) {
            setTimeout(function () {
              wx.hideLoading()
              wx.stopPullDownRefresh() //停止下拉刷新
            }, 0)
            setTimeout(function () {
              wx.showModal({
                title: '您的账号已在其他设备登录，请重新登录！',
                showCancel: false, //是否显示取消按钮false去掉取消按钮
                success: function (res) {
                  if (res.confirm) {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  } else {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  }
                }
              })
            }, 0)
            return
          }
          console.log(res)
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
          if (res.data.result == 0) {
            wx.setStorageSync("yue", res.data.balance);
            that.setData({
              showModal: false,
              allmoney: res.data.balance,
              weixininfo:'',
              jine:'',
              yinhangkainfo:''
            })
            wx.navigateTo({
              url: '/pages/setting/page/tixiansuccess/tixiansuccess',
            })
            // setTimeout(function() {
            //   wx.navigateBack({
            //     delta: 1
            //   })
            // }, 1500)
          }
        }
      });
    }
  },
  // add弹窗
  close_mask: function() {
    this.setData({
      showModal: false
    })
  },
  // 微信提现
  weixintixian() {
    let that = this
    that.setData({
      type: 2
    })
    if (!that.data.weixininfo) {
      wx.showToast({
        title: '请选择微信账户',
        icon: 'none'
      })
      return
    }
    if (!that.data.jine) {
      wx.showToast({
        title: '请输入提现金额',
        icon: 'none'
      })
      return
    }
    if (wx.getStorageSync('yue')<that.data.jine){
      wx.showToast({
        title: '提现金额不足，请重新填写',
        icon: 'none'
      })
      return
    }
    if (that.data.jine < 1) {
      wx.showToast({
        title: '微信提现金额最少为1元',
        icon: 'none'
      }) 
      return
    }
    // 显示密码弹窗
    that.setData({
      showModal: true,
      isFocus: true, //控制input 聚焦
      mima:''
    })

  },
  // 银行卡提现
  tinhangtixian() {
    let that = this
    that.setData({
      type: 1
    })
    if (!that.data.yinhangkainfo) {
      wx.showToast({
        title: '请选择银行卡',
        icon: 'none'
      })
      return
    }
    if (!that.data.jine) {
      wx.showToast({
        title: '请输入提现金额',
        icon: 'none'
      })
      return
    }
    if (wx.getStorageSync('yue') < that.data.jine) {
      wx.showToast({
        title: '提现金额不足，请重新填写',
        icon: 'none'
      })
      return
    }
    if (that.data.jine < 2) {
      wx.showToast({
        title: '银行卡提现金额最少为2元',
        icon: 'none'
      })
      return
    }
    // 显示密码弹窗
    that.setData({
      showModal: true,
      isFocus: true, //控制input 聚焦
      mima: ''
    })

  },
  // 姓名
  shoukuanren(e) {
    // console.log(e.detail.value)
    this.setData({
      personname: e.detail.value
    })
  },
  // 金额
  moneynum(e) {
    // console.log(e.detail.value)
    this.setData({
      jine: e.detail.value
    })
  },
  moneyShijiao(e) {
    let that = this
    let values = e.detail.value
    if (values.indexOf('.') != -1) {
      values = (values * 1).toFixed(2)
      if (e.detail.value.length > values.length){
        wx.showToast({
          title: '最多保留两位小数，金额为' + values,
          icon: 'none'
        })
      }
    }
    if (values == 'NaN') {
      wx.showToast({
        title: '请输入正确的提现金额',
        icon: 'none'
      })
      values = ''
    }
    that.setData({
      jine: values
    })
  },
  // 提现记录
  toTixianjilu() {
    wx.navigateTo({
      url: '/pages/setting/page/tixianjilu/tixianjilu',
    })
  },
  // 选择微信
  chooseweixin() {
    wx.navigateTo({
      url: '/pages/setting/page/chooseweixin/chooseweixin',
    })
  },

  // 切换头
  changeS(e) {
    let that = this
    that.setData({
      scrIndex: e.currentTarget.dataset.index,
      jine: ''
    })
  },
  //选择银行卡
  bindShowMsg() {
    wx.navigateTo({
      url: '/pages/setting/page/chooseyinhangk/chooseyinhangk',
    })
  },
  // 体现全部
  fiexallmoney(){
    this.setData({
      jine: this.data.allmoney
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      showModal: false, //add弹窗
      mima: '', //密码
      isFocus: false, //控制input 聚焦
      allmoney: wx.getStorageSync('yue')
    })
    let that = this
    if (wx.getStorageSync('paypasswordtype') == 1) {
      wx.showModal({
        title: '提示',
        content: '还没有交易密码，请先设置交易密码',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/setting/page/shezhi/shoucijiaoyimima/shoucijiaoyimima',
            })
          } else if (res.cancel) {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 100)
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '众商联采',
      path: "/pages/login/login", // 路径，传递参数到指定页面。
      // imageUrl:'../../../images/logo/LOGO.png'
      imageUrl: app.globalData.logo
    }
  }
})